package com.ruoyi.system;


import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.async.ResultCallbackTemplate;
import com.github.dockerjava.api.command.InfoCmd;
import com.github.dockerjava.api.model.*;
import com.github.dockerjava.core.*;
import com.github.dockerjava.okhttp.OkDockerHttpClient;
import com.jayway.jsonpath.JsonPath;
import it.corsinvest.proxmoxve.api.PveClient;
import it.corsinvest.proxmoxve.api.Result;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Closeable;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws Exception{


        docker();
//        pve();
    }

    public static void docker() throws Exception{

        String a = "tcp://10.10.10.201:50001";


        OkDockerHttpClient build = new OkDockerHttpClient.Builder().dockerHost(new URI(a)).build();



        DockerClient dockerClient = DockerClientImpl.getInstance(new DockerClientConfig() {
            @Override
            public URI getDockerHost() {
                try {
                    return new URI(a);
                } catch (URISyntaxException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public RemoteApiVersion getApiVersion() {
                return RemoteApiVersion.UNKNOWN_VERSION ;
            }

            @Override
            public String getRegistryUsername() {
                return null;
            }

            @Override
            public String getRegistryPassword() {
                return null;
            }

            @Override
            public String getRegistryEmail() {
                return null;
            }

            @Override
            public String getRegistryUrl() {
                return null;
            }

            @Override
            public AuthConfig effectiveAuthConfig(String imageName) {
                return null;
            }

            @Override
            public AuthConfigurations getAuthConfigurations() {
                return null;
            }

            @Override
            public SSLConfig getSSLConfig() {
                return null;
            }
        },build);


        List<Image> exec = dockerClient.listImagesCmd().withShowAll(true).exec();

        System.out.println(exec);

    }

    public static void pve() throws Exception{

        //if you want use lite version only get/set/create/delete use PveClientBase

        PveClient client = new PveClient("pve.indsvr.com", 443);
        if (client.login("root", "Lyq521..", "pam")) {
            //version
//            System.out.println(client.getVersion().version().getResponse().get("data"));

//            // same for put/post/delete
//            //loop nodes for

//            JSONObject jsonObject = (JSONObject) client.getNodes().index().getResponse().getJSONArray("data").get(0);
//
//            System.out.println(jsonObject);

//
//            //loop nodes for each
//            PveClient.<JSONObject>JSONArrayToList(client.getNodes().index().getResponse().getJSONArray("data")).forEach((node) -> {
//                System.out.println(node);
//            });
//
//


//            JSONArray vms = client.getNodes().get("pve").getQemu().vmlist().getResponse().getJSONArray("data");
//            for (int i = 0; i < vms.length(); i++) {
//                System.out.println(vms.get(i));
//            }
//            JSONArray lxcs = client.getNodes().get("pve").getLxc().vmlist().getResponse().getJSONArray("data");
//            for (int i = 0; i < lxcs.length(); i++) {
//                System.out.println(lxcs.get(i));
//            }

//             列出硬盘
//            JSONArray jsonArray = client.getNodes().get("pve").getDisks().getList().list().getResponse().getJSONArray("data");
//            for (Object o : jsonArray) {
//                System.out.println(o);
//            }

//            JSONArray jsonArray = client.getStorage().index().getResponse().getJSONArray("data");

//            JSONArray jsonArray = client.getStorage().index().getResponse().getJSONArray("data");
//            for (Object o : jsonArray) {
//                System.out.println(o);
//            }

//            //所有lxc
//            Object o = client.getNodes().get("pve").getLxc().vmlist().getResponse().get("data");
//            System.out.println(o);
//
////          所有虚拟机
//             o = client.getNodes().get("pve").getQemu().vmlist().getResponse().get("data");



//            System.out.println(o);

            JSONObject pve = client.getNodes().get("pve").getQemu().get("204").getVncproxy().vncproxy().getResponse();
//            HashMap<String, Object> parameters = new HashMap<>();
//            parameters.put("generate-password",true);
//            JSONObject pve = client.create("/nodes/pve/qemu/204/vncproxy", parameters).getResponse();

            System.out.println(pve);


            JSONObject data = (JSONObject) pve.get("data");
            Result vncwebsocket = client.getNodes().get("pve").getQemu().get("204")
                    .getVncwebsocket().vncwebsocket(data.getInt("port"), data.getString("ticket"));
            JSONObject response = vncwebsocket.getResponse();
            System.out.println(response);
            client.getNodes().get("pve").getVncshell().vncshell();

        }
    }


}
